﻿package mortal.game.scene3D.buff
{
    import Message.BroadCast.*;
    import Message.Public.*;
    import baseEngine.modifiers.*;
    import com.gengine.debug.*;
    import frEngine.effectEditTool.temple.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.player.weapon.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.mvc.core.*;

    public class ChainEffect extends Object
    {
        protected var effectPlayer:EffectPlayer;
        protected var url:String;
        protected var player:SpritePlayer;
        protected var entityId:SEntityId;
        protected var fromPlayer:SpritePlayer;
        protected var fromEntityId:SEntityId;
        protected var isRemoveToPlayerDispose:Boolean = false;

        public function ChainEffect(param1:String, param2:SpritePlayer, param3:SEntityId, param4:Boolean = false)
        {
            this.isRemoveToPlayerDispose = param4;
            this.url = param1;
            this.player = param2;
            if (!this.player.entityInfo)
            {
                return;
            }
            this.entityId = this.player.entityInfo.entityInfo.entityId;
            this.fromEntityId = param3;
            this.fromPlayer = ThingUtil.entityUtil.getEntity(this.fromEntityId) as SpritePlayer;
            Dispatcher.addEventListener(EventName.Scene_AddEntity, this.onSceneAddEntity);
            if (this.fromPlayer)
            {
                this.makeChain();
            }
            return;
        }// end function

        protected function onSceneAddEntity(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityInfo;
            if (EntityUtil.equal(_loc_2.entityId, this.fromEntityId))
            {
                this.fromPlayer = ThingUtil.entityUtil.getEntity(this.fromEntityId) as SpritePlayer;
                this.makeChain();
            }
            return;
        }// end function

        protected function onSceneRemoveEntity(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            if (EntityUtil.equal(_loc_2, this.entityId) || this.isRemoveToPlayerDispose && EntityUtil.equal(_loc_2, this.fromEntityId))
            {
                this.dispose();
            }
            else if (EntityUtil.equal(_loc_2, this.fromEntityId))
            {
                this.removeChain();
            }
            return;
        }// end function

        protected function makeChain() : void
        {
            var _loc_1:WeaponPlayer = null;
            if (this.effectPlayer)
            {
                return;
            }
            this.effectPlayer = EffectPlayerPool.instance.getEffectPlayer(this.url, this.player.bodyPlayer, null, true);
            Log.debug("添加锁链：", this.player.entityInfo.entityInfo.name);
            if (this.effectPlayer.temple is TempleFight)
            {
                if (this.player is UserPlayer)
                {
                    _loc_1 = (this.player as UserPlayer).weaponPlayer;
                }
                if (!this.fromPlayer.bodyPlayer || !this.player.bodyPlayer)
                {
                    return;
                }
                (this.effectPlayer.temple as TempleFight).setFightParams(this.fromPlayer.bodyPlayer, this.player.bodyPlayer, _loc_1, false, null, -1);
                this.effectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
            }
            Dispatcher.removeEventListener(EventName.Scene_AddEntity, this.onSceneAddEntity);
            Dispatcher.addEventListener(EventName.Scene_RemoveEntity, this.onSceneRemoveEntity);
            return;
        }// end function

        protected function removeChain() : void
        {
            if (this.effectPlayer)
            {
                Log.debug("移除锁链：", this.player.entityInfo.entityInfo.name);
                this.effectPlayer.parent = null;
                this.effectPlayer.dispose();
                this.effectPlayer = null;
            }
            Dispatcher.addEventListener(EventName.Scene_AddEntity, this.onSceneAddEntity);
            return;
        }// end function

        public function dispose() : void
        {
            Dispatcher.removeEventListener(EventName.Scene_AddEntity, this.onSceneAddEntity);
            Dispatcher.removeEventListener(EventName.Scene_RemoveEntity, this.onSceneRemoveEntity);
            this.isRemoveToPlayerDispose = false;
            this.url = "";
            this.player = null;
            this.entityId = null;
            this.fromPlayer = null;
            this.fromEntityId = null;
            if (this.effectPlayer)
            {
                Log.debug("Dispose锁链：");
                this.effectPlayer.parent = null;
                this.effectPlayer.dispose();
                this.effectPlayer = null;
            }
            return;
        }// end function

    }
}
